/*
 * @file     g_timer.h
 * @brief    source file for setting timer
 *
 * Change Logs:
 * Date            Author             Version        Notes
 * 2020-12-29      wanghongwei        V1.0.0         the first version
 */

#ifndef __G_TIMER_H__
#define __G_TIMER_H__

#include "g_1903.h"
#include "g_system.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define PCNT(TIMx)                (TIMx * 2)
#define NCNT(TIMx)                ((TIMx * 2) + 1)

/**
 * @brief  pwm startlevel definition
 */
typedef enum
{
    LOW_LEVEL = 0,
    HIGH_LEVEL,
} START_Level;

/**
 * @brief  timer Numble enable Structure definition
 */
typedef enum
{
    TIM0 = 0,
    TIM1,
    TIM2,
    TIM3,
    TIM4,
    TIM5,
    TIM6,
    TIM7,
    TIM8
} G_TIM_NumTypeDef;
#define IS_TIMER_NUM(TIMx) (TIMx <= TIM8)
#define IS_TIMM(TIMMx) (TIMMx == MTIM)

/**
 * @brief  timer Init Structure definition
 */
typedef struct
{
    G_TIM_NumTypeDef TIMx;
    G_FunctionalState Auto_Reload;
    uint32_t period;
} G_TIM_InitTypeDef;

/**
 * @brief  PWM Init Structure definition
 */
typedef struct
{
    G_TIM_NumTypeDef TIMx;
    uint32_t LowLevelPeriod;
    uint32_t HighLevelPeriod;
    START_Level StartLevel;
} G_PWM_InitTypeDef;

/**
 * @brief  timer mode Structure definition
 */
typedef enum
{
    TIM_Mode_PWM = 0,
    TIM_Mode_TIMER = 1
} G_TIM_ModeTypeDef;
#define IS_TIM_MODE(mode) (mode == TIM_Mode_TIMER || mode == TIM_Mode_PWM)

void delay_ms(int ms);
void delay_us(int us);
void G_TIM_Init(TIM_Module_TypeDef *TIMMx, G_TIM_InitTypeDef *TIM_init_struct);
void G_TIM_DeInit(TIM_Module_TypeDef *TIMMx);
void G_TIM_Cmd(TIM_Module_TypeDef *TIMMx, G_TIM_NumTypeDef TIMx, G_FunctionalState NewState);
void G_TIM_ModeConfig(TIM_Module_TypeDef *TIMMx, G_TIM_NumTypeDef TIMx, G_TIM_ModeTypeDef TIM_Mode);
void G_TIM_SetPeriod(TIM_Module_TypeDef *TIMMx, G_TIM_NumTypeDef TIMx, uint32_t Period);
void G_TIM_PWMInit(TIM_Module_TypeDef *TIMMx, G_PWM_InitTypeDef *PWM_init_struct);
void G_TIM_SetPWMPeriod(TIM_Module_TypeDef *TIMMx, G_TIM_NumTypeDef TIMx, uint32_t LowLevelPeriod, uint32_t HighLevelPeriod);
void G_TIM_ITConfig(TIM_Module_TypeDef *TIMMx, G_TIM_NumTypeDef TIMx, G_FunctionalState NewState);
void G_TIM_ClearITPendingBit(TIM_Module_TypeDef *TIMMx, G_TIM_NumTypeDef TIMx);
G_ITStatus G_TIM_GetITStatus(TIM_Module_TypeDef *TIMMx, G_TIM_NumTypeDef TIMx);
uint32_t G_TIM_GetAllITStatus(TIM_Module_TypeDef *TIMMx);

#ifdef __cplusplus
}
#endif

#endif /*__G_TIMER_H__*/

/************************ (C) COPYRIGHT GKT *****END OF FILE****/
